package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/*
* 体检预约服务
* */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
//分页查询(根据条件)
//分页查询体检预约信息
@RequestMapping("/pageQuery")
public Result pageQuery(@RequestBody QueryPageBean queryPageBean) {
    try {
        PageResult pageResult = orderService.pageQuery(queryPageBean);
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, pageResult);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
    }
}

    //新增体检订单
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, String[] setmealId) {
        if (setmealId == null || setmealId.length != 1) {
            return new Result(false,"套餐必须且只能选一个,请检查");
        }else {
            map.put("setmealId",setmealId[0]);
        }
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            return orderService.order(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //删除体检订单
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.deleteById(id);
            return new Result(true, "删除预约订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除预约订单失败");
        }
    }
    //批量删除
    @RequestMapping("/deleteAll")
    public Result deleteAll(@RequestBody List<Map<Object,Object>> sels){
        try {
            for (Map<Object, Object> map : sels) {
                Integer id = (Integer) map.get("id");
                orderService.deleteById(id);
            }
            return new Result(true, "批量删除预约订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"批量删除预约订单失败");
        }
    }
    //确定预约
    @RequestMapping("/sendMessage2Confirm")
    public Result sendMessage2Confirm(String phoneNumber,String orderDate,Integer id) {
        try {
            System.out.println("id:"+id);
            System.out.println("phoneNumber:"+phoneNumber);
            System.out.println("orderDate:"+orderDate);
            orderService.updateById(id);
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,phoneNumber,orderDate);
            return new Result(true,"预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"预约失败");
        }
    }
}
